/**
 * FileName: EmployeeServiceImpl.java
 * 员工业务实现类
 *
 * @author  王威
 * @Date    2021/12/1
 * @version 1.00
 **/
package com.site.service.impl;

import com.site.dao.EmployeeDao;
import com.site.dao.EmployeeRoleDao;
import com.site.dao.impl.EmployeeDaoImpl;
import com.site.dao.impl.EmployeeRoleDaoImpl;
import com.site.pojo.Employee;
import com.site.pojo.EmployeeRole;
import com.site.service.EmployeeRoleService;
import com.site.service.EmployeeService;
import com.site.utils.JDBCUtils;
import com.site.vo.UserControlVO;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class EmployeeServiceImpl implements EmployeeService {
    private EmployeeDao employeeDao = new EmployeeDaoImpl();
    private EmployeeRoleDao employeeRoleDao = new EmployeeRoleDaoImpl();

    @Override
    public Employee selectByname(Employee employee) throws SQLException {
        return employeeDao.selectByname(employee);
    }

    @Override
    public Employee selectById(Employee employee) throws SQLException {
        return employeeDao.selectById(employee);
    }

    @Override
    public List<Employee> selectAll() throws SQLException {
        return employeeDao.selectAll();
    }

    /*添加事务提交*/
    @Override
    public boolean addEmployee(Employee employee, EmployeeRole employeeRole) throws SQLException {
        Connection con = JDBCUtils.getConnection();
        boolean falg = false;
        con.setAutoCommit(false);
        int num02 = employeeDao.addEmployee(con,employee);
        int num01 = employeeRoleDao.addEmployeeRole(con,employeeRole);
        if (num01==1&&num02==1){
            falg = true;
            con.commit();
        }else con.rollback();
        JDBCUtils.close(null,null,con);

        return falg;
    }

    @Override
    public boolean updateEmlyee(Employee employee, EmployeeRole employeeRole) {
        return false;
    }
    /*删除员工表*/
    @Override
    public boolean delEmplyee(Employee employee, EmployeeRole employeeRole) throws SQLException {
        boolean falg = false;
        Connection con = JDBCUtils.getConnection();
        con.setAutoCommit(false);
        int num1 = employeeRoleDao.delEmployeeRole(con,employeeRole);
        int num2 = employeeDao.delEmplyee(con,employee);
        if (num1>0&&num2>0){
            falg = true;
            con.commit();
        }else {
            con.rollback();
        }
        System.out.println("删除："+falg);
        System.out.println("角色表删除："+num1);
        System.out.println("员工表删除："+num2);
        JDBCUtils.close(null,null,con);
        return falg;
    }



    @Override
    public boolean addEmployee(Connection con, Employee employee) throws SQLException {
        return employeeDao.addEmployee(con,employee)>0?true:false;
    }

    @Override
    public boolean delEmplyee(Connection con, Employee employee) {
        return employeeDao.delEmplyee(con,employee)>0?true:false;
    }



    @Override
    public boolean updateEmlyee(Connection con, Employee employee) {
        return employeeDao.updateEmlyee(con,employee)>0?true:false;
    }



    /*用户登录*/
    @Override
    public Employee login(Employee employee) throws SQLException {
        return employeeDao.login(employee);
    }

    @Override
    public List<UserControlVO> selectUserVO() throws SQLException {
        return employeeDao.selectUserVO();
    }
}
